package com.ruoyi.project.purchase.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.ResultBean;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.domain.RyDepotHead;
import com.ruoyi.project.purchase.domain.RyPurchaseOrder;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.mapper.RyDepotDetailMapper;
import com.ruoyi.project.purchase.mapper.RyDepotHeadMapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.warehouse.domain.RyDepotItem;
import com.ruoyi.project.warehouse.mapper.RyDepotItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.purchase.mapper.RyQualityMapper;
import com.ruoyi.project.purchase.domain.RyQuality;
import com.ruoyi.project.purchase.service.IRyQualityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购质检Service业务层处理
 *
 * @author you
 * @date 2022-09-25
 */
@Service
public class RyQualityServiceImpl implements IRyQualityService
{
    @Autowired
    private RyQualityMapper ryQualityMapper;

    @Autowired
    private RyDepotDetailMapper ryDepotDetailMapper;

    @Autowired
    private RyDepotHeadMapper ryDepotHeadMapper;

    @Autowired
    private RyDepotItemMapper ryDepotItemMapper;


    /**
     * 查询采购质检
     *
     * @param id 采购质检主键
     * @return 采购质检
     */
    @Override
    public RyQuality selectRyQualityById(Long id)
    {
        return ryQualityMapper.selectRyQualityById(id);
    }

    /**
     * 查询采购质检列表
     *
     * @param ryQuality 采购质检
     * @return 采购质检
     */
    @Override
    public List<RyQuality> selectRyQualityList(RyQuality ryQuality)
    {
        return ryQualityMapper.selectRyQualityList(ryQuality);
    }

    /**
     * 新增采购质检
     *
     * @param ryQuality 采购质检
     * @return 结果
     */
    @Override
    public int insertRyQuality(RyQuality ryQuality)
    {
        ryQuality.setCreateTime(DateUtils.getNowDate());
        return ryQualityMapper.insertRyQuality(ryQuality);
    }

    /**
     * 新增采购质检
     * 实质上只新增了采购质检主表，子表数据只需要修改相应信息即可
     */
    @Override
    @Transactional
    public AjaxResult addDepotHeadAndDetail(DepotHeadItem depotHeadItem){
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //新增购质检单信息
        RyQuality ryQuality = new RyQuality();
        ryQuality.setNumber(depotHeadItem.getNumber());//单据号
        ryQuality.setFileName(depotHeadItem.getFileName());
        ryQuality.setSalesMan(depotHeadItem.getSalesMan());
        ryQuality.setCreateTime(DateUtils.getNowDate());//创建时间
        ryQuality.setCreator(user.getUserId());//操作员
        ryQuality.setRemark(depotHeadItem.getRemark());
        ryQuality.setType("其他");
        ryQuality.setStatus("0");//默认未审核
        ryQuality.setSubType("采购质检");

        //判断是否关联了采购订单
        if(depotHeadItem.getLinkHeadId() != null) {//关联了采购订单
            ryQuality.setLinkHeadId(depotHeadItem.getLinkHeadId());//关联采购订单id
            ryQuality.setLinkNumber(depotHeadItem.getNumber());
            //查询采购单信息
            RyDepotHead ryDepotHead = ryDepotHeadMapper.selectRyDepotHeadById(depotHeadItem.getLinkHeadId());
            if(ryDepotHead.getProjectHeadId() != null){//如果采购订单中存在项目总清单，则关联了项目
                ryQuality.setProjectHeadId(ryDepotHead.getProjectHeadId());
            }

        }

        int res = ryQualityMapper.insertRyQuality(ryQuality);
        if(res > 0){
            //遍历明细数据，更新单据明细数据
            for(RyDepotItem ryDepotItem : tableData){
                ryDepotItem.setHeaderId(ryQuality.getId());//设置刚生成的采购订单id
                ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
            }
        }

        return AjaxResult.success("新增成功");
    }

    /**
     * 根据主表修改单据子表信息
     */
    @Transactional
    @Override
    public AjaxResult editDepotDetail(DepotHeadItem depotHeadItem){
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //质检模块只需要修改个别信息即可
        RyQuality ryQuality = new RyQuality();
        ryQuality.setId(depotHeadItem.getId());//主键，用于定位修改数据
        ryQuality.setOperTime(DateUtils.getNowDate());
        ryQuality.setFileName(depotHeadItem.getFileName());
        ryQuality.setRemark(depotHeadItem.getRemark());
        //修改
        ryQualityMapper.updateRyQuality(ryQuality);

        //将子表数据先删除，根据主表id删除
        ryDepotItemMapper.deleteDepotItemByHeadId(depotHeadItem.getId());
        //遍历明细数据，新增单据明细数据
        for(RyDepotItem ryDepotItem : tableData){
            ryDepotItem.setHeaderId(depotHeadItem.getId());//设置刚生成的采购订单id
            ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
        }

        return AjaxResult.success("修改成功");
    }



    /**
     * 查询采购质检单据主表及子表信息
     *
     * @param id 单据主表主键
     * @return 单据主表及子表信息
     */
    @Override
    public DepotHeadItem selectRyDepotDetailById(Long id)
    {
        DepotHeadItem depotHeadItem = new DepotHeadItem();
        RyQuality ryQuality = ryQualityMapper.selectRyQualityById(id);
        //主表数据
        depotHeadItem.setId(ryQuality.getId());
        depotHeadItem.setNumber(ryQuality.getNumber());//单据号

        depotHeadItem.setCreator(ryQuality.getCreator());//操作员
        depotHeadItem.setOperName(ryQuality.getOperName());//操作员名称

        depotHeadItem.setCreateTime(ryQuality.getCreateTime());
        depotHeadItem.setOperTime(ryQuality.getOperTime());
        depotHeadItem.setFileName(ryQuality.getFileName());
        depotHeadItem.setSalesMan(ryQuality.getSalesMan());

        depotHeadItem.setProjectName(ryQuality.getProjectName());//项目名
        depotHeadItem.setProjectCode(ryQuality.getProjectCode());//项目代号
        depotHeadItem.setContractNumber(ryQuality.getContractNumber());//合同号

        depotHeadItem.setLinkNumber(ryQuality.getLinkNumber());//关联订单号
        depotHeadItem.setLinkHeadId(ryQuality.getLinkHeadId());//关联订单id
        depotHeadItem.setRemark(ryQuality.getRemark());//备注

        //子表数据
        List<RyDepotItem> tableData = ryDepotItemMapper.selectQualityItemByDepotId(id);
        depotHeadItem.setTableData(tableData);

        return depotHeadItem;
    }

    @Override
    public List<RyDepotItem> selectOrderItemByDepotId(Long id){
        return ryDepotItemMapper.selectOrderItemByDepotId(id);
    }



    /**
     * 根据订单id查询关联采购单
     * @param orderId
     * @return
     */
    public int selectCountByOrderId(Long orderId){
        return ryQualityMapper.selectCountByOrderId(orderId);
    }




    /**
     * 修改采购质检
     *
     * @param ryQuality 采购质检
     * @return 结果
     */
    @Override
    public int updateRyQuality(RyQuality ryQuality)
    {
        return ryQualityMapper.updateRyQuality(ryQuality);
    }

    /**
     * 批量删除采购质检
     *
     * @param ids 需要删除的采购质检主键
     * @return 结果
     */
    @Override
    public int deleteRyQualityByIds(Long[] ids)
    {
        return ryQualityMapper.deleteRyQualityByIds(ids);
    }

    /**
     * 删除采购质检信息
     *
     * @param id 采购质检主键
     * @return 结果
     */
    @Override
    public int deleteRyQualityById(Long id)
    {
        return ryQualityMapper.deleteRyQualityById(id);
    }
}
